import sklearn.datasets as dataset
from sklearn.cluster import KMeans
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

x_train,target = dataset.make_blobs(n_samples=200,centers=3)

kmn = KMeans(n_clusters=3)
kmn.fit(x_train)
y_new = kmn.predict(x_train)

centers = kmn.cluster_centers_


plt.figure(figsize=(16,9))
plt.subplot(221)
plt.scatter(x_train[:,0],x_train[:,1],c=y_new) #yucefenlei
plt.scatter(centers[:,0],centers[:,1],c='r',s=100,alpha=0.6)
plt.subplot(222)
plt.scatter(x_train[:,0],x_train[:,1],c=target)
plt.savefig('k.png')